<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mahasiswa extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_mahasiswa');
		$this->load->model('mdl_fakultas');
		$this->load->model('mdl_prodi');
	}
	
	function index(){
		$this->load->view('master/mahasiswa');
	}
	
	function grid(){
		$data = $this->mdl_mahasiswa->getdata();
		echo $this->mdl_mahasiswa->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['kd_mahasiswa'] = '';
		$data['thn_angkatan'] = '';
		$data['nim'] = '';
		$data['nama'] = '';
		$data['tempat_lahir'] = '';
		$data['tgl_lahir'] = date('d/m/Y');
		$data['kd_jk'] = '';
		$data['kd_prov'] = '';
		$data['kd_kabup'] = '';		
		$data['alamat'] = '';
		$data['no_telp'] = '';
		$data['kd_prodi'] = '';
		$data['kd_program'] = '';
		$data['kd_dosen'] = '';
		$data['nama_ortu'] = '';
		$data['alamat_ortu'] = '';
		$data['pekerjaan'] = '';
		$data['telp_ortu'] = '';
		$data['passwd'] = '';
        $data['user_level_id'] = '';
		$data['status'] = 'A';
		$this->load->view('master/mahasiswa_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_mahasiswa->getdataedit($kode);
		
		$data['kd_mahasiswa'] = $kode;
		$data['thn_angkatan'] = $r->row()->thn_angkatan;
		$data['nim'] = $r->row()->nim;
		$data['nama'] = $r->row()->nama;
		$data['tempat_lahir'] = $r->row()->tempat_lahir;
		$data['tgl_lahir'] = FormatDateFromMysql($r->row()->tgl_lahir);
		$data['kd_jk'] = $r->row()->kd_jk;
		$data['kd_kabup'] = $r->row()->kd_kabup;
		$data['kd_prov'] = $r->row()->kd_prov;
		$data['alamat'] = $r->row()->no_telp;
		$data['no_telp'] = $r->row()->alamat;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['kd_program'] = $r->row()->kd_program;
		$data['kd_dosen'] = $r->row()->kd_dosen;
		$data['nama_ortu'] = $r->row()->nama_ortu;
		$data['alamat_ortu'] = $r->row()->alamat_ortu;
		$data['pekerjaan'] = $r->row()->pekerjaan;
		$data['telp_ortu'] = $r->row()->telp_ortu;
		$data['passwd'] = $r->row()->passwd;
        $data['user_level_id'] = $r->row()->user_level_id;
		$data['status'] = $r->row()->status;	
		$this->load->view('master/mahasiswa_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("thn_angkatan", 'Tahun Angkatan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nim", 'NIM', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat", 'alamat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("no_telp", 'Telepon Siswa', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Program Studi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_program", 'Program', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_dosen", 'Dosen Wali', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("nama_ortu", 'Nama Orang Tua', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("alamat_ortu", 'Alamat Orang Tua', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("pekerjaan", 'Pekerjaan', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("telp_ortu", 'telp_ortu', 'trim|required|xss_clean');
		$this->form_validation->set_rules("status", 'Status', 'trim|required|xss_clean');		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_mahasiswa->InsertOnDb($data);
			}else { // edit
				$result=$this->mdl_mahasiswa->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	
	function delete($kode){
		$result = $this->mdl_mahasiswa->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} 
	
	/*function pdf(){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_mahasiswa->get_pdf();
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}
		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',16);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Data Mahasiswa';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Mahasiswa.pdf", "I");
	}*/
	
	function laporan_pdf($kd_prodi, $thn_angkatan){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('L', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['thn_angkatan'] = $thn_angkatan;
		
		 //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_mahasiswa->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('master/mahasiswa_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('mahasiswa.pdf');
	}
	
	function laporan_excel() {			
		$query = $this->db->get('tbl_mahasiswa');
			$this->excel_generator->set_query($query);
			$this->excel_generator->set_header(array('Kode', 'Thn Angkatan', 'NIM', 'Nama', 'Tempat Lahir', 'Tgl Lhir', 'Jns Kelamin', 'Kota', 'Alamat', 'No Telp', 'Prodi', 'Program', 'Dosen', 'Nama Ortu', 'Alamat Ortu', 'Pekerjaan', 'Telp Ortu', 'Status'));
			$this->excel_generator->set_column(array('kd_mahasiswa', 'thn_angkatan', 'nim', 'nama', 'tempat_lahir', 'tgl_lahir', 'kd_jk', 'kd_kabup', 'alamat', 'no_telp', 'kd_prodi', 'kd_program', 'kd_dosen', 'nama_ortu', 'alamat_ortu', 'pekerjaan', 'telp_ortu', 'status'));
			$this->excel_generator->set_width(array(10, 10, 10, 25, 13, 10, 10, 15, 15, 10, 20, 10, 10, 15, 15, 15, 10, 8));
			$this->excel_generator->exportTo2003('Mahasiswa');
	}
	
		function import(){
		# --
		$error='';
		$result=true;
		$extensi = '';
		
		# load
		$this->load->helper('file');
		
		# upload filenya
		$fupload = $_FILES['datafile'];
		$nama = $_FILES['datafile']['name'];
		$extensi = $_FILES['datafile']['type'];
		//chan
		  $allowedExtensions = array("xls"); 
       
			  if (!in_array(end(explode(".", 
					strtolower($nama))), 
					$allowedExtensions)) { 
					echo json_encode(array('success'=>false, 'msg'=>'File yang akan disuport hanya untuk tipe Excel (xls)'));
				
					return;
			   
			  } 
			  
			  
		if(isset($fupload)){
			$lokasi_file 	= $fupload['tmp_name'];
			$direktori		= "file_upload/master_data/$nama";
			
			if(move_uploaded_file($lokasi_file, $direktori)){ // proses upload					
				//Excel 2003
					# load librari
					$this->load->library('excel');
					$this->excel->setOutputEncoding('CP1251');
					$this->excel->read($direktori); // baca file
					
					# baca per baris
					for($i=2, $n=$this->excel->rowcount(0); $i<= $n; $i++){
						# data
						$data['kd_mahasiswa'] 		= $this->excel->val($i, 1);
						$data['thn_angkatan'] 		= $this->excel->val($i, 2);
						$data['nim'] 				= $this->excel->val($i, 3);
						$data['nama'] 				= $this->excel->val($i, 4);
						$data['tempat_lahir'] 		= $this->excel->val($i, 5);
						$data['tgl_lahir'] 			= $this->excel->val($i, 6);
						$data['kd_jk'] 				= $this->excel->val($i, 7);
						$data['kd_kabup'] 			= $this->excel->val($i, 8);
						$data['alamat'] 			= $this->excel->val($i, 9);
						$data['no_telp'] 			= $this->excel->val($i, 10);
						$data['kd_prodi'] 			= $this->excel->val($i, 11);
						$data['kd_program'] 		= $this->excel->val($i, 12);
						$data['kd_dosen'] 			= $this->excel->val($i, 13);
						$data['nama_ortu'] 			= $this->excel->val($i, 14);
						$data['alamat_ortu'] 		= $this->excel->val($i, 15);
						$data['pekerjaan'] 			= $this->excel->val($i, 16);
						$data['telp_ortu'] 			= $this->excel->val($i, 17);
						$data['status'] 			= $this->excel->val($i, 18);
						//$data['passwd'] 			= $this->excel->val($i, 19);
						//$data['user_lvevel_id'] 	= $this->excel->val($i, 20);
						# proses
						$prodis = array();
						$filtered = false;
						$skip = false;
						if($this->input->post('kd_fakultas') != NULL && $this->input->post('kd_fakultas') != '') {
							if($this->input->post('kd_prodi') != NULL && $this->input->post('kd_prodi') != '') {
								$prodis[] = $this->input->post('kd_prodi');
							} else {
								$xxx = $this->mdl_prodi->getProdiByFak($this->input->post('kd_fakultas'));
								if($xxx) {
									$res = $xxx->result();
									foreach($res as $xx) {
										$prodis[] = $xx->kd_prodi;
									}
								}
							}
							$filtered = true;
						}
						if($filtered) { $skip = true; foreach($prodis as $prod) {
							if($prod == $data['kd_prodi']) {
								$skip = false;
							}
						}}
						if($skip) {
							continue;
						}
						$result=$this->mdl_mahasiswa->importData($data);
						if (!$result) $error = "Gagal Import, kemungkinan data yang diimport sudah ada pada database";
					}
					
				/* }else{
					$result=false;
					$error = "Format File tidak diketahui.";
				} */
				
				# clear folder temporari
				delete_files('restore');
				
			}else{
				$result=false;
				$error = "Gagal upload";
			}
		}
		
		if ($result){
			echo json_encode(array('success'=>true, 'msg'=>'Data berhasil diimport'));
		} else {
			echo json_encode(array('msg'=>$error));
		}
		
	}
}